package com.example.jwt.demo.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

@Aspect//标识这是一个切面类
@Component
public class InvokeLogAspect {
    @Pointcut("@annotation(com.example.jwt.demo.aop.InvokeLog)")
    public void pt(){
    }
    @Around("pt()")
    public Object printInvokeLog(ProceedingJoinPoint joinPoint){
        Object proceed=null;
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        String methodName = signature.getMethod().getName();
        System.out.println(methodName+"即将被调用");
        try {
            proceed = joinPoint.proceed();
            System.out.println(methodName+"被调用完了");
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            System.out.println(methodName+"出现了异常");
        }
        return proceed;
    }
}
